/**$Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *   Copyright (C) 2008 - 2013 xingsheng.zhu@gmail.com All rights reserved.
 */
package com.blazemina.robot;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import com.blazemina.config.BlazeminaConfig;
import com.blazemina.config.BlazeminaConfigManager;
import com.blazemina.message.Message;
import com.blazemina.mina.Connector;
import com.blazemina.robot.connector.ConnectorManager;

/**
 * @author JackChu
 * @version 1.0.0 2013-3-4 上午11:17:54
 */
public abstract class AbstractRobot {

	private Executor executor = Executors.newCachedThreadPool();

	private BlazeminaConfig blazeminaConfig;

	private void init() {
		blazeminaConfig = configBlazemina();
		BlazeminaConfigManager.me().init(blazeminaConfig);
		ConnectorManager.init(BlazeminaConfigManager.me().getRobotNums());
	}

	protected void start() {
		init();
		int robotNums = BlazeminaConfigManager.me().getRobotNums();

		for (int i = 0; i < robotNums; i++) {
			executor.execute(new Runnable() {
				@Override
				public void run() {
					Connector connector = ConnectorManager.getConnector();
					connector.start();
					connector.send(getMessage());
				}
			});

			try {
				Thread.sleep(BlazeminaConfigManager.me().getInterval());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	protected abstract Message getMessage();

	protected abstract BlazeminaConfig configBlazemina();
}
